/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.schnipsel.swt.editor;

import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import de.mnit.basis.daten.struktur.container.Engpass;
import de.mnit.basis.swt.element.feld.menue.SWT_MenueFeld;
import de.mnit.basis.swt.element.menue.SWT_Menue_PopUp;
import de.mnit.basis.swt.element.steuer.rollbar.gruppe.bild.SWT_TextEditor;
import de.mnit.basis.swt.grafik.icon.direkt.ICON;
import de.mnit.basis.sys.Ausgabe;
import de.mnit.basis.sys.sprache.S;
import de.mnit.schnipsel.swt.dialog.K_WebLink;
import de.mnit.schnipsel.swt.editor.basis.LinkInfo;
import de.mnit.schnipsel.swt.editor.basis.TextEditor;
import de.mnit.schnipsel.zentral.SC_GuiObs;

/**
 * @author Michael Nitsche
 * 24.09.2010	Erstellt
 */
public class EditorPopUp {

	private static final S s=new S(EditorPopUp.class);
	private final SWT_TextEditor editor;
	private final TextEditor textEditor;


	public EditorPopUp(TextEditor textEditor) {
		this.textEditor=textEditor;
		this.editor = textEditor.gEditor();
	}


	public void init() {
		final StyledText styledText=editor.swt();

		// MENU
		SWT_Menue_PopUp menu=editor.nMenu_PopUp();
		menu.nMenueFeld(s.t("Ausschneiden"), null, ICON._16.AKTION_AUSSCHNEIDEN).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				textEditor.ausschneiden();
			}
		});
		menu.nMenueFeld(s.t("Kopieren"), null, ICON._16.AKTION_KOPIEREN).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				textEditor.kopieren();
			}
		});
		menu.nMenueFeld(s.t("Einfügen"), null, ICON._16.AKTION_EINFUEGEN).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				textEditor.einfuegen();
			}
		});
		menu.nMenueTrenner();
		final Engpass<Point> mausPos=Engpass.neu();
		final SWT_MenueFeld mLink=menu.nMenueFeld(s.t("Link bearbeiten"));
		mLink.eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				if(mausPos.g()==null)
					return;
				Point maus=mausPos.g();
				StyleRange sr=editor.gOffsetStyle(maus.x, maus.y);
				boolean istLink=sr!=null && sr.data!=null && sr.data.getClass().equals(LinkInfo.class);
				if(istLink) {
					LinkInfo link=(LinkInfo)sr.data; //TODO ArtikelLink
					String titel=editor.swt().getTextRange(sr.start, sr.length);
					link=new LinkInfo(titel, link.art, link.daten); //TODO Hm???

					K_WebLink dialog=new K_WebLink();
					dialog.setze(link);
					LinkInfo linkNeu=dialog.gErgebnis(SC_GuiObs.i().hauptfenster());
					if(linkNeu==null)
						return;

					Ausgabe.debug(linkNeu.titel, linkNeu.daten, link);
					//TODO Siehe den bei LinkNeu
					if(!link.titel.equals(linkNeu.titel)) {
						styledText.replaceTextRange(sr.start, sr.length, linkNeu.titel);
						sr.length=linkNeu.titel.length();
					}
					sr.data=linkNeu;
					styledText.replaceStyleRanges(sr.start, sr.length, new StyleRange[]{sr});

					if(!link.titel.equals(linkNeu.titel) || !link.daten.equals(linkNeu.daten))
						SC_GuiObs.i().anzeige().sGeaendert(true);
				}
			}
		});

		menu.eZeigen(new Listener() {
			public void handleEvent(Event e) {
		        Point maus = editor.swt().getDisplay().getCursorLocation();
		        Point loc=editor.swt().toControl(maus);
		        Point rel=new Point(loc.x, loc.y);
		        mausPos.s(rel);
				StyleRange sr=editor.gOffsetStyle(rel.x, rel.y);
				try {
					boolean istLink=sr!=null && sr.data!=null && sr.data.getClass().equals(LinkInfo.class);
					mLink.sAktiv(istLink);
				}
				catch(IllegalArgumentException t) {
					mLink.sAktiv(false);
				}
			}
		});
	}

}
